package com.tnstart.android.control.light.config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ConfigLoader {

	public static ConfigBean loadConfigXml(String configXmlFile) {
		ConfigBean configBean = new ConfigBean();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(ConfigLoader.class
					.getResourceAsStream(configXmlFile));
			Element root = document.getDocumentElement();
			NodeList childNodes = root.getChildNodes();
			for (int j = 0; j < childNodes.getLength(); j++) {
				if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
					if ("host".equals(childNodes.item(j).getNodeName())) {
						configBean.setHost(childNodes.item(j).getFirstChild()
								.getNodeValue());
					} else if ("port".equals(childNodes.item(j).getNodeName())) {
						configBean.setPort(Integer.parseInt(childNodes.item(j)
								.getFirstChild().getNodeValue()));
					}
				}
			}
		} catch (ParserConfigurationException e) {

		} catch (SAXException e) {

		} catch (IOException e) {
		}
		return configBean;
	}

	public static List<ItemBean> loadItemXml(String configXmlFile) {
		List<ItemBean> itemsArr = new ArrayList<ItemBean>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(ConfigLoader.class
					.getResourceAsStream(configXmlFile));
			Element root = document.getDocumentElement();
			NodeList itemNodes = root.getElementsByTagName("item");
			for (int i = 0; i < itemNodes.getLength(); i++) {
				Element itemElement = (Element) itemNodes.item(i);
				NodeList childNodes = itemElement.getChildNodes();
				ItemBean itemBean = new ItemBean();
				for (int j = 0; j < childNodes.getLength(); j++) {
					
					if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
						if ("name".equals(childNodes.item(j).getNodeName())) {
							itemBean.setItemDefaultName(childNodes.item(j)
									.getFirstChild().getNodeValue());
						} else if ("id"
								.equals(childNodes.item(j).getNodeName())) {
							itemBean.setItemId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("typeid".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setItemTypeId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("areaid".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setAreaId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("netnum".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setNetNum(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("devicenum".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setDeviceNum(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("controltype".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setControlType(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("circuitnum".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setCircuitNum(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						}
					}					
				}
				itemsArr.add(itemBean);
			}
		} catch (ParserConfigurationException e) {

		} catch (SAXException e) {

		} catch (IOException e) {
		}
		return itemsArr;

	}
	
	public static List<AreaBean> loadAreaXml(String configXmlFile) {
		List<AreaBean> itemsArr = new ArrayList<AreaBean>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(ConfigLoader.class
					.getResourceAsStream(configXmlFile));
			Element root = document.getDocumentElement();
			NodeList itemNodes = root.getElementsByTagName("area");
			for (int i = 0; i < itemNodes.getLength(); i++) {
				Element itemElement = (Element) itemNodes.item(i);
				NodeList childNodes = itemElement.getChildNodes();
				AreaBean itemBean = new AreaBean();
				for (int j = 0; j < childNodes.getLength(); j++) {
					
					if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
						if ("name".equals(childNodes.item(j).getNodeName())) {
							itemBean.setAreaName(childNodes.item(j)
									.getFirstChild().getNodeValue());
						} else if ("id"
								.equals(childNodes.item(j).getNodeName())) {
							itemBean.setAreaId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} else if ("typeid".equals(childNodes.item(j)
								.getNodeName())) {
							itemBean.setAreaTypeId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						}
					}					
				}
				itemsArr.add(itemBean);
			}
		} catch (ParserConfigurationException e) {

		} catch (SAXException e) {

		} catch (IOException e) {
		}
		return itemsArr;

	}
	
	public static List<ItemTypeBean> loadItemTypeXml(String configXmlFile) {
		List<ItemTypeBean> itemsArr = new ArrayList<ItemTypeBean>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(ConfigLoader.class
					.getResourceAsStream(configXmlFile));
			Element root = document.getDocumentElement();
			NodeList itemNodes = root.getElementsByTagName("itemtype");
			for (int i = 0; i < itemNodes.getLength(); i++) {
				Element itemElement = (Element) itemNodes.item(i);
				NodeList childNodes = itemElement.getChildNodes();
				ItemTypeBean itemBean = new ItemTypeBean();
				for (int j = 0; j < childNodes.getLength(); j++) {
					
					if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
						if ("name".equals(childNodes.item(j).getNodeName())) {
							itemBean.setItemTypeName(childNodes.item(j)
									.getFirstChild().getNodeValue());
						} else if ("id"
								.equals(childNodes.item(j).getNodeName())) {
							itemBean.setItemTypeId(Integer.parseInt(childNodes
									.item(j).getFirstChild().getNodeValue()));
						} 
					}					
				}
				itemsArr.add(itemBean);
			}
		} catch (ParserConfigurationException e) {

		} catch (SAXException e) {

		} catch (IOException e) {
		}
		return itemsArr;

	}

}
